package com.sx.jys.ui.act

import android.os.Bundle
import android.widget.EditText
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import com.blankj.utilcode.util.ThreadUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.util.KeyboardUtils
import com.sanxinsaas.nearstore.framework.util.click
import com.sanxinsaas.nearstore.framework.util.hideSoftKeyboard
import com.sos.widget.app.dialog.HintDialog
import com.sos.widget.view.refresh.refreshLoadListener
import com.sx.jys.R
import com.sx.jys.app.framework.BaseActivity
import com.sx.jys.app.net.bean.FxBeanList
import com.sx.jys.app.until.toRateNoUntil
import com.sx.jys.app.until.toRateSmall
import com.sx.jys.databinding.ActFxControlBinding
import com.sx.jys.ui.adapter.ItemFkListAdapter
import com.sx.jys.ui.vm.FkVm

class FxListAct : BaseActivity<FkVm, ActFxControlBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        getImmersionBar().titleBar(titleBar).init()
        mDatabind.myRefresh.refreshLoadListener({
            mViewModel.reqList()
        }, {

        })
    }

    override fun initData() {
        mDatabind.vm = mViewModel
        mDatabind.adapter = ItemFkListAdapter(this).also {
            it.setOnItemClickListener { viewId, item, position ->
                val dialog = XPopup.Builder(this)
                    .autoOpenSoftInput(false)
                    .dismissOnTouchOutside(false)
                    .asCustom(object : BasePopupView(this) {
                        override fun getInnerLayoutId(): Int = R.layout.view_switch_pop
                        override fun onCreate() {
                            super.onCreate()
                            val tvCode = findViewById<TextView>(R.id.tv_code)
                            val close = findViewById<ImageView>(R.id.image_close)
                            val edManyRate = findViewById<EditText>(R.id.ed_many_rate)
                            val edManyValue = findViewById<EditText>(R.id.ed_many_value)
                            val switchMany = findViewById<Switch>(R.id.switch_many)
                            val edEmptyRate = findViewById<EditText>(R.id.ed_empty_rate)
                            val edEmptyValue = findViewById<EditText>(R.id.ed_empty_value)
                            val switchEmpty = findViewById<Switch>(R.id.switch_empty)
                            val tvConfirm = findViewById<TextView>(R.id.tv_confirm)
                            tvCode.setText(item.code.orEmpty())
                            edManyRate.setText(item.manyStopProportion.toRateNoUntil())
                            edManyRate.setSelection(edManyRate.text.toString().length)
                            edManyValue.setText(item.manyStopPrice)
                            switchMany.isChecked = item.manySwitchStatus()
                            edEmptyRate.setText(item.emptyStopProportion.toRateNoUntil())
                            edEmptyValue.setText(item.emptyStopPrice)
                            switchEmpty.isChecked = item.emptySwitchStatus()
                            edManyRate.isEnabled=false
                            edManyValue.isEnabled=false
                            edEmptyRate.isEnabled=false
                            edEmptyValue.isEnabled=false
                            close.click {
                                dialog.dismiss()
                            }
                            tvConfirm.click {
                                val bean = FxBeanList()
                                bean.code = item.code
                                bean.id = item.id
                                bean.manyStopPrice = edManyValue.text.toString()
                                bean.manyStopProportion = edManyRate.text.toString().toRateSmall()
                                bean.manySwitch = if (switchMany.isChecked) "0" else "1"
                                bean.emptyStopPrice = edEmptyValue.text.toString()
                                bean.emptyStopProportion = edEmptyRate.text.toString().toRateSmall()
                                bean.emptySwitch = if (switchEmpty.isChecked) "0" else "1"
                                mViewModel.reqSettingCur(bean)
                                dialog.dismiss()
                            }
                            ThreadUtils.getMainHandler().postDelayed({
                                edManyRate.isEnabled=true
                                edManyValue.isEnabled=true
                                edEmptyRate.isEnabled=true
                                edEmptyValue.isEnabled=true
                            },30)

                        }

                    })
                    .show()
            }
        }
        mViewModel.reqList()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.finishRefresh.observe(this) {
            mDatabind.myRefresh.finishRefresh()
        }
        mViewModel.hintSuccessEvent.observe(this){
            HintDialog.Builder(this)
                .setIcon(HintDialog.ICON_FINISH)
                .setMessage("设置成功!")
                .show()
        }
    }
}